<script setup>
const props = defineProps(['shopName', 'product', 'min'])
import { useCartStore } from '@/stores/cart.js'

const cart = useCartStore()

const deleteProduct = (product) => {
    if(product.number > 0) {
        product.number--
    }
    // 在购物车列表中查找当前店铺购物车
    let res = cart.cartList.find(item => item.name == props.shopName)
    if(!res) return
    if(product.number == 0) {
        res.list = res.list.filter(item => item !== product)
    }
}
const addProduct = (product) => {
    product.number++
    // 在购物车列表中查找当前店铺购物车
    let res = cart.cartList.find(item => item.name == props.shopName)
    if(!res) {
        // 创建一个店铺购物车
        let shopCart = { name: '', list: [] }
        // 命名为当前店铺购物车
        shopCart.name = props.shopName
        // product.number++
        // 把商品添加到购物车
        shopCart.list.push(product)
        // 把当前店铺购物车添加到购物车列表
        cart.cartList.push(shopCart)
        console.log(cart.cartList)
    }else {
        // product.number++
        // 在当前店铺购物车中查找当前商品
        let res2 = res.list.find(item => item == product)
        if(!res2) {
            // product.number++
            res.list.push(product)
        }else {
            // product.number++
        }
        console.log(cart.cartList)
    }
}
</script>

<template>
    <div class="product">
        <div :class="[ props.min == 'min' ? 'product_img--min' : 'product_img']">
            <img :src="`http://www.dell-lee.com/imgs/vue3/${ props.product.imgName }.png`" alt="">
        </div>
        <div class="product_content">
            <div class="product_content_title">{{ props.product.title }}</div>
            <!-- <div class="product_content_sales">月售 {{ props.product.sales }} 件</div> -->
            <slot></slot>
            <div class="product_content_price">￥{{ props.product.price }} <span class="product_content_old_price">￥{{ props.product.oldPrice }}</span></div>
            
        </div>
        <div class="product_number">
            <span class="product_number_minus" @click="deleteProduct(props.product)">-</span>
            {{ props.product.number }}
            <span class="product_number_plus" @click="addProduct(props.product)">+</span>
        </div>
    </div>
</template>

<style lang="scss" scoped>
@import '@/style/viriables.scss';
@import '@/style/mixins.scss';
.product {
    padding: .18rem;
    box-sizing: border-box;
    display: flex;
    position: relative;
    border-bottom: .01rem solid $content-bgColor;

    &_img {
        width: .68rem;
        margin-right: .18rem;

        img {
            width: .68rem;
            height: .68rem;
        }
    }
    &_img--min {
        width: .48rem;
        margin-right: .18rem;

        img {
            width: .48rem;
            height: .48rem;
        }
    }
    &_content {
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: space-between;

        &_title {
            font-size: .14rem;
            font-weight: bold;
            color: $content_fontcolor;
            @include ellipse;
        }
        &_price {
            color: $hightlight-fontColor;
        }
        &_old_price {
            color: $light-fontColor;
            text-decoration: line-through;
        }
    }
    &_number {
        position: absolute;
        right: .18rem;
        bottom: .18rem;

        &_minus,&_plus {
            display: inline-block;
            width: .2rem;
            height: .2rem;
            border-radius: 50%;
            font-size: .16rem;
            text-align: center;
        }
        &_minus {
            border: .01rem solid $medium-fontColor;
            color: $medium-fontColor;
            margin-right: .08rem;
        }
        &_plus {
            background-color: $btn-bgColor;
            color: $bgColor;
            margin-left: .08rem;
        }
    }
}
</style>